<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Discount_type_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $amount = 0;
	var $unit = 0;
	var $name = '';
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setAmount($input){
		$this->amount = $input;
	}
	function setUnit($input){
		$this->unit = $input;
	}
	function setName($input){
		$this->name = $input;
	}
	function getId(){
		return $this->id;
	}
	function getAmount(){
		return $this->amount;
	}
	function getUnit(){
		return $this->unit;
	}
	function getName(){
		return $this->name;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_DISCOUNT_TYPE)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->amount = $result[0]->amount;
			$this->unit = $result[0]->unit;
			$this->name = $result[0]->name;
			return $this;
		}
		return false;
	}
	
	function getList($limit = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('unit', 'DESC');
		return $ci->db->get(TBL_DISCOUNT_TYPE)->result();
	}
	function count(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->count_all_results(TBL_DISCOUNT_TYPE);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('amount',$this->getAmount());
		$ci->db->set('unit',$this->getUnit());
		$ci->db->set('name',$this->getName());
		$ci->db->insert(TBL_DISCOUNT_TYPE);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('amount',$this->getAmount());
		$ci->db->set('unit',$this->getUnit());
		$ci->db->set('name',$this->getName());
		return $ci->db->where('id', $this->id)->update(TBL_DISCOUNT_TYPE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_DISCOUNT_TYPE);
	}
}